﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using BUS;
using DTO;
using System.Web.Services;
namespace AirPlane
{
    public partial class QuanLyChangBay : System.Web.UI.Page
    {
        protected ChangBayBUS bus = new ChangBayBUS();
        protected SanBayBUS sb = new SanBayBUS();

        protected DataTable dt = new DataTable();
        protected DataTable dt1 = new DataTable();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["islogin"] == null || (bool)Session["islogin"] == false || Session["type"].ToString() != "2")
            {

                Response.Redirect("TrangChu.aspx");
            }
            else
            {
                if (!IsPostBack)
                {
                    ddlSanBayDi.DataSource = sb.loadSanBayDi();
                    ddlSanBayDi.DataBind();
                    ddlSanBayDen.DataSource = sb.loadSanBayDen();
                    ddlSanBayDen.DataBind();
                    fillData();
                    fillDataDopDowlistFooter();


                }
                lbChangBay.Text = "";
            }
        }

        protected void gwChangBay_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            gwChangBay.EditIndex = -1;
            fillData();
            fillDataDopDowlistFooter();
        }

        protected void gwChangBay_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            string maChang = gwChangBay.DataKeys[e.RowIndex].Value.ToString();
            bus.capNhatChangBay(maChang);
            fillData();
            fillDataDopDowlistFooter();
        }

        protected void gwChangBay_RowEditing(object sender, GridViewEditEventArgs e)
        {
            gwChangBay.EditIndex = e.NewEditIndex;
            string sanbaydi = ((Label)gwChangBay.Rows[e.NewEditIndex].Cells[1].FindControl("lbSanBayDi")).Text;
            string sanbayden = ((Label)gwChangBay.Rows[e.NewEditIndex].Cells[2].FindControl("lbSanBayDen")).Text;
            fillData();
            
            DropDownList ddlSanBayDiQL1 = (DropDownList)gwChangBay.Rows[e.NewEditIndex].Cells[1].FindControl("ddlSanBayDiQL");
            DropDownList ddlSanBayDenQL1 = (DropDownList)gwChangBay.Rows[e.NewEditIndex].Cells[2].FindControl("ddlSanBayDenQL");
            dt = sb.loadSanBayDi();
            ddlSanBayDiQL1.DataSource = dt;
            ddlSanBayDiQL1.DataTextField = "tenSanBay";
            ddlSanBayDiQL1.DataValueField = "ID_sanBayDi";
            ddlSanBayDiQL1.DataBind();
          
            ddlSanBayDenQL1.DataSource = sb.loadSanBayDen();
            ddlSanBayDenQL1.DataTextField = "tenSanBay";
            ddlSanBayDenQL1.DataValueField = "ID_sanBayDen";
            ddlSanBayDenQL1.DataBind();
            for(int i= 0; i < dt.Rows.Count;i++ )
            {
                if (dt.Rows[i]["tenSanBay"].ToString().Equals(sanbaydi))
                {
                    ddlSanBayDiQL1.SelectedIndex = i;
                    break;
                }
            }
             for(int i= 0; i < dt.Rows.Count;i++ )
            {
                if (dt.Rows[i]["tenSanBay"].ToString().Equals(sanbayden))
                {
                    ddlSanBayDenQL1.SelectedIndex = i;
                    break;
                }
            }
             fillDataDopDowlistFooter();
            
        }

        protected void gwChangBay_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            if (e.NewValues == e.OldValues)
            {
                e.Cancel = true;
            }
            else
            {
                ChangBayDTO dto = new ChangBayDTO();
                string maChang = gwChangBay.DataKeys[e.RowIndex].Value.ToString();
                string maSanBayDi = ((DropDownList)gwChangBay.Rows[e.RowIndex].Cells[1].FindControl("ddlSanBayDiQL")).SelectedValue.ToString();
                string maSanBayDen  = ((DropDownList)gwChangBay.Rows[e.RowIndex].Cells[2].FindControl("ddlSanBayDenQL")).SelectedValue.ToString();
                float soDam = float.Parse(((TextBox)gwChangBay.Rows[e.RowIndex].Cells[3].FindControl("TextBox1")).Text);
                float nhienLieuDuKien = float.Parse(((TextBox)gwChangBay.Rows[e.RowIndex].Cells[4].FindControl("TextBox2")).Text);
                int n = 0;
                dto.MaChangBay = maChang;
                dto.MaSanBayDi = maSanBayDi;
                dto.MaSanBayDen = maSanBayDen;
                dto.SoDam = soDam;
                dto.NhienLieuDuKien = nhienLieuDuKien;
                n = bus.capNhatChangBay(dto);

                gwChangBay.EditIndex = -1;
                fillData();
                fillDataDopDowlistFooter();
            }
          



        }
        public void fillData()
        {
            gwChangBay.DataSource = bus.layThongTinChangBay();
            gwChangBay.DataBind();
        }
        public void fillDataDopDowlistFooter()
        {
            DropDownList ddlSanBayDiQL1 = (DropDownList)gwChangBay.FooterRow.FindControl("ddlSanBayDiQL_insert");
            DropDownList ddlSanBayDenQL1 = (DropDownList)gwChangBay.FooterRow.FindControl("ddlSanBayDenQL_insert");
            dt = sb.loadSanBayDi();
            ddlSanBayDiQL1.DataSource = dt;
            ddlSanBayDiQL1.DataTextField = "tenSanBay";
            ddlSanBayDiQL1.DataValueField = "ID_sanBayDi";
            ddlSanBayDiQL1.DataBind();

            ddlSanBayDenQL1.DataSource = sb.loadSanBayDen();
            ddlSanBayDenQL1.DataTextField = "tenSanBay";
            ddlSanBayDenQL1.DataValueField = "ID_sanBayDen";
            ddlSanBayDenQL1.DataBind();
        }
      
        protected void gwChangBay_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gwChangBay.PageIndex = e.NewPageIndex;
            fillData();

            fillDataDopDowlistFooter();
        }

        protected void imbt_insert_Click(object sender, ImageClickEventArgs e)
        {
           string maSanBayDi =  ((DropDownList)gwChangBay.FooterRow.FindControl("ddlSanBayDiQL_insert")).SelectedValue.ToString();
           string maSanBayDen = ((DropDownList)gwChangBay.FooterRow.FindControl("ddlSanBayDenQL_insert")).SelectedValue.ToString();
           string soDam = ((TextBox)gwChangBay.FooterRow.FindControl("txtsoDam")).Text;
           string nhienLieuDuKien = ((TextBox)gwChangBay.FooterRow.FindControl("txtNhienLieu")).Text;
            bool flag = true;
            if (maSanBayDi == maSanBayDen)
            {
                lbChangBay.Text = "<span id = 'thong-bao-ket-Qua-Tim-Kiem'>Sân Bay Đên Và Sân Bay Đi Phải Khác Nhau!</span>";
                flag = false;
            }
            else

               if (bus.kiemTraChangBay(maSanBayDi, maSanBayDen) == false)
               {
                   lbChangBay.Text = "<span id = 'thong-bao-ket-Qua-Tim-Kiem'>Chặng Bay Này Đã Có Rồi! Vui Lòng Kiểm Tra Lại</span>";
                   flag = false;
               }
               else
                   if (soDam == "" || nhienLieuDuKien == "")
                   {
                       lbChangBay.Text = " <span id = 'thong-bao-ket-Qua-Tim-Kiem'>Số Dặm Hoặc Nhiên Nhiên Liệu Dự Kiến Không Được Bỏ Trống.</span>";
                       flag = false;
                   }
                   else
                   {
                       if (soDam.Length > 0)
                       {
                           for (int i = 0; i < soDam.Length; i++)
                           {
                               if (soDam[i] < 48 || soDam[i] > 58)
                               {
                                   flag = false;
                                   lbChangBay.Text = " <span id = 'thong-bao-ket-Qua-Tim-Kiem'>Số Dặm Phải Là Một Số!.</span>";
                                   break;
                               }

                           }
                       }
                       if (nhienLieuDuKien.Length > 0)
                       {
                           for (int i = 0; i < nhienLieuDuKien.Length; i++)
                           {
                               if (nhienLieuDuKien[i] < 48 || nhienLieuDuKien[i] > 58)
                               {
                                   flag = false;
                                   lbChangBay.Text = " <span id = 'thong-bao-ket-Qua-Tim-Kiem'>Số Nhiên Liệu Phải Là Một Số!.</span>";
                                   break;
                               }

                           }
                       }
                   }
                       
           if (flag == true)
           {
               int n = 0;
               ChangBayDTO dto = new ChangBayDTO();
               dto.MaSanBayDi = maSanBayDi;
               dto.MaSanBayDen = maSanBayDen;
               dto.SoDam = float.Parse(soDam);
               dto.NhienLieuDuKien = float.Parse(nhienLieuDuKien);
               n = bus.themChangBay(dto);
               fillData();
               fillDataDopDowlistFooter();
           }
                  
        }
        [WebMethod]
        public static string timKiemChangBay(string maSanBayDi,string maSanBayDen)
        {
            string s1 = "";
            DataTable dt2 = new DataTable();
            ChangBayBUS cb = new ChangBayBUS();
            dt2 = cb.layThongTinChangBay(maSanBayDi, maSanBayDen);
            if (dt2.Rows.Count == 0)
            {
                s1 = "<span id = 'thong-bao-ket-Qua-Tim-Kiem'>Không có kết quả nào được tìm thấy!.</span>";
               
            }
            else
            {
                s1 += "<table id ='table-b'>";
                s1 += "<tr >";
                s1 += "<th width= '150px'>Mã Chặng Bay </th>";
                s1 += "<th width= '200px'>Sân Bay Đi</th>";
                s1 += "<th width= '200px'>Sân Bay Đến </th>";
                s1 += "<th width= '100px'>Số Dặm </th>";
                s1 += "<th width= '200px'>Nhiên Liệu Dự Kiến</th>";




                s1 += "</tr>";

                foreach (DataRow dr in dt2.Rows)
                {
                    string maChangBay = dr["ID_changBay"].ToString();
                    string tenSanBayDi = dr["tenSanBayDi"].ToString();
                    string tenSanBayDen = dr["tenSanBayDen"].ToString();
                    string soDam = dr["soDam"].ToString();
                    string nhienLieu = dr["nhienLieuDuKien"].ToString();



                    s1 += "<tr >";

                    s1 += "<td width= '150px'>" + maChangBay + "</td>";
                    s1 += "<td width= '200px'>" + tenSanBayDi + "</td>";
                    s1 += "<td width= '200px'>" + tenSanBayDen + "</td>";
                    s1 += "<td width= '100px'>" + soDam + " </td>";
                    s1 += "<td width= '200px'>" + nhienLieu + "</td>";

                    s1 += "</tr>";
                }
                s1 += "</table>";

            }
            return s1;
        }
    }
}